﻿using FANUC.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class PartCount
    {
        public string Part_Name { get; set; }
        public int TotalQuantity { get; set; }
    }

    public class TodayRobotOutputsByPartController : ApiController
    {
        private FANUCDB db = new FANUCDB();

        [ResponseType(typeof(TodayRobotOutputsByPartsDtoOutput))]
        public IHttpActionResult Get(int id)
        {
            #region Test
            if (Helper.IsTest)
            {
                TodayRobotOutputsByPartsDtoOutput testData = new TodayRobotOutputsByPartsDtoOutput();
                testData.Date = System.DateTime.Now.Date;

                testData.SlicesInfo = new List<TodayRobotOutputsByPartsSliceDto>();
                TodayRobotOutputsByPartsSliceDto info1 = new TodayRobotOutputsByPartsSliceDto() { PartNumber = "PN1", Ratio = 10.5M, Quantity = 105 };
                TodayRobotOutputsByPartsSliceDto info2 = new TodayRobotOutputsByPartsSliceDto() { PartNumber = "PN2", Ratio = 20M, Quantity = 200 };
                TodayRobotOutputsByPartsSliceDto info3 = new TodayRobotOutputsByPartsSliceDto() { PartNumber = "PN3", Ratio = 30M, Quantity = 300 };
                TodayRobotOutputsByPartsSliceDto info4 = new TodayRobotOutputsByPartsSliceDto() { PartNumber = "PN4", Ratio = 39.5M, Quantity = 395 };
                testData.SlicesInfo.Add(info1);
                testData.SlicesInfo.Add(info2);
                testData.SlicesInfo.Add(info3);
                testData.SlicesInfo.Add(info4);

                return Ok(testData);
            }
            #endregion

            DateTime today = System.DateTime.Now.Date;
            var robot = db.Robot.Where(a => a.ID == id).FirstOrDefault();
            var outputsByPartSummary = (from w in db.Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                                        group w by new { w.Part_Name } into grp
                                        select new
                                        {
                                            grp.Key.Part_Name,
                                            TotalQuantity = grp.Count()
                                        }).OrderByDescending(a => a.TotalQuantity);
            switch (robot.Robot_Type.Type)
            {
                case "Spot Welding":
                    outputsByPartSummary = (from w in db.Spot_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                                           group w by new { w.Part_Name } into grp
                                           select new
                                           {
                                               grp.Key.Part_Name,
                                               TotalQuantity = grp.Count()
                                           }).OrderByDescending(a => a.TotalQuantity);
                    break;
                case "Seal":
                    outputsByPartSummary = (from w in db.Seal_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                                           group w by new { w.Part_Name } into grp
                                           select new
                                           {
                                               grp.Key.Part_Name,
                                               TotalQuantity = grp.Count()
                                           }).OrderByDescending(a => a.TotalQuantity);
                    break;
                case "Press":
                    outputsByPartSummary = (from w in db.Press_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                                           group w by new { w.Part_Name } into grp
                                           select new
                                           {
                                               grp.Key.Part_Name,
                                               TotalQuantity = grp.Count()
                                           }).OrderByDescending(a => a.TotalQuantity);
                    break;
                case "Polish":
                    outputsByPartSummary = (from w in db.Polish_Part_Unit.Where(a => a.Start_Time >= today && a.Robot_ID == id)
                                           group w by new { w.Part_Name } into grp
                                           select new
                                           {
                                               grp.Key.Part_Name,
                                               TotalQuantity = grp.Count()
                                           }).OrderByDescending(a => a.TotalQuantity);
                    break;
            }
         
            if (outputsByPartSummary.Count() <= 0)
            {
                TodayRobotOutputsByPartsDtoOutput noData = new TodayRobotOutputsByPartsDtoOutput();
                noData.Date = System.DateTime.Now.Date;

                noData.SlicesInfo = new List<TodayRobotOutputsByPartsSliceDto>();
                TodayRobotOutputsByPartsSliceDto info = new TodayRobotOutputsByPartsSliceDto() { PartNumber = "", Ratio = 0M, Quantity = 0 };
                noData.SlicesInfo.Add(info);
                return Ok(noData);
            }

            int todayTotalQuantity = outputsByPartSummary.Sum(a => a.TotalQuantity);

            TodayRobotOutputsByPartsDtoOutput data = new TodayRobotOutputsByPartsDtoOutput();
            data.Date = System.DateTime.Now.Date;
            data.SlicesInfo = new List<TodayRobotOutputsByPartsSliceDto>();
            foreach (var item in outputsByPartSummary)
            {
                data.SlicesInfo.Add(new TodayRobotOutputsByPartsSliceDto() { PartNumber = item.Part_Name, Ratio = Decimal.Round(Convert.ToDecimal(item.TotalQuantity) / todayTotalQuantity, 4), Quantity = item.TotalQuantity });
            }

            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        { return null; }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
